
const db = require('../db/echarts.js')

exports.save_currentEchartsList = (req,res)=>{
  console.log('req.body',req.body)
  const req_body_name = req.body.name
  const req_body_list = req.body.list
  const req_body_img = req.body_img.fileUrl
  
  // 定义数据库查询语句
  const sqlInsertCanvas = 'insert into ev_canvas (name,img) VALUES (?)'
  const sqlStr = 'select * from ev_echart_config where _id = ?'

  db.query(sqlInsertCanvas,[req_body_name,req_body_img],(err,saveCanvasRes)=>{
    if(err) return res.cc(err)
    console.log('saveCanvasRes',saveCanvasRes)
    req_body_list.forEach((echart)=>{
      console.log("echart",echart)
      db.query(sqlStr,echart.id,(err,echartRes)=>{
        if(err) return res.cc(err)
        if(echartRes.length>0) return res.cc()

        const sqlInsert = 'insert into ev_echart_config set ?'
        const sqlInsertOpt = 'insert into ev_echart_config_option set ?'
        
        db.query(sqlInsert,
          {
            type:echart.type,
            x:echart.x,
            y:echart.y,
            width:echart.width,
            height:echart.height,
            canvas_id:saveCanvasRes.insertId},(err,configRes)=>{
              if(err) return res.cc(err)
              if(configRes.affectedRows !==1) return res.cc('保存失败')
              db.query(sqlInsertOpt,{
                backgroundColor:echart.option.backgroundColor,
                titile_text:echart.option.title.text,
                xAxis:JSON.stringify(echart.option.xAxis.data),
                color:JSON.stringify(echart.option.color),
                
                yAxis:JSON.stringify(echart.option.yAxis.data),
                _id:configRes.insertId,
              },(err,results)=>{
                if(err){
                  return res.cc(err)
                }else if(results.affectedRows !==1){
                  return res.cc('保存失败')
                }else{
                  res.cc('保存成功')
                }
              })
              
            })
      })

    })
  })
  
  
}

exports.getEchartCaves=(req,res)=>{
  const req_body_param = req.body

  const sqlGetCaves = 'select * from '


}